﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using Module.Common.Models;
using Module.Core.DAO;
using Module.Core.EntitiesInfo;
using Module.Core.DAO.DAO;
using System.Web;

namespace Module.Core.BusinessLogic.ShopingCart
{
  public  class ShoppingCart
  {
     static HttpCookie shopingcartCookies;
      
      private static double _subTotal;
      private static double _total;
    

      public ShoppingCart()
      {
          shopingcartCookies  = new HttpCookie("shopingcart");
          shopingcartCookies.Expires = DateTime.Now.AddHours(1);
          //Response.Cookies.Add(StudentCookies);
          
      }
     
       private static CHRTList<ShopingCartItemInfo> _items = new CHRTList<ShopingCartItemInfo>();

       public static CHRTList<ShopingCartItemInfo> Items
       {
           get { return ShoppingCart._items; }
           set { ShoppingCart._items = value; }
       }
       private static bool _isDirty = false ;
       public static void AddItem(int id, string namge, int quatity, double unitPrice)
       {
             

           _isDirty = true;
           foreach (ShopingCartItemInfo item in _items)
           {
               if (item.Id == id)
               {
                   item.Quantity += quatity;
                   return;
               }

           }
           _items.Add(new ShopingCartItemInfo { Id = id, Name = namge, Quantity = quatity, UnitPrice = unitPrice });


       }
       public static void RemoveItem(int id)
       {
           foreach (ShopingCartItemInfo item in _items)
           {
               if (item.Id == id)
               {
                   _items.Remove(item);
                   _isDirty = true;
                   return;
               }
           }
                   
           }
       public static void UpdateQuantity(int id , int quantity)
       {
           foreach(ShopingCartItemInfo item in _items)
           {
               if(item.Id == id)
               {
                   item.Quantity = quantity;
                   _isDirty = true;
                   return ;
                }
           }
}
       public static void ReCalcullator()
       {
           if(!_isDirty)
               return;
           _subTotal = 0.0;
           _total = 0.0;
           foreach(ShopingCartItemInfo item in _items)
           {
               _subTotal += item.Quantity * item.UnitPrice;
           }
           _total = _subTotal;
       }
       public static double Total
       {
           get
           {
               ReCalcullator();
               return _total;
           }
       }
      }
    }

